package com.ruoyi.training.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.training.domain.lang.TrainingIpPoolInfo;
import com.ruoyi.training.domain.lang.enums.TrainingIpPoolStatus;
import com.ruoyi.training.domain.dto.TrainingIpPoolDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.training.mapper.TrainingIpPoolMapper;
import com.ruoyi.training.domain.TrainingIpPool;
import com.ruoyi.training.service.ITrainingIpPoolService;
import org.springframework.transaction.annotation.Transactional;

/**
 * IP池Service业务层处理
 *
 * @author rocc
 * @date 2025-06-12
 */
@Service
public class TrainingIpPoolServiceImpl implements ITrainingIpPoolService {
    @Autowired
    private TrainingIpPoolMapper trainingIpPoolMapper;

    /**
     * 查询IP池
     *
     * @param id IP池主键
     * @return IP池
     */
    @Override
    public TrainingIpPool selectTrainingIpPoolById(Long id) {
        return trainingIpPoolMapper.selectTrainingIpPoolById(id);
    }

    /**
     * 查询IP池列表
     *
     * @param trainingIpPool IP池
     * @return IP池
     */
    @Override
    public List<TrainingIpPool> selectTrainingIpPoolList(TrainingIpPool trainingIpPool) {
        return trainingIpPoolMapper.selectTrainingIpPoolList(trainingIpPool);
    }

    /**
     * 新增IP池
     *
     * @param trainingIpPool IP池
     * @return 结果
     */
    @Override
    public int insertTrainingIpPool(TrainingIpPool trainingIpPool) {
        return trainingIpPoolMapper.insertTrainingIpPool(trainingIpPool);
    }

    /**
     * 修改IP池
     *
     * @param trainingIpPool IP池
     * @return 结果
     */
    @Override
    public int updateTrainingIpPool(TrainingIpPool trainingIpPool) {
        return trainingIpPoolMapper.updateTrainingIpPool(trainingIpPool);
    }

    /**
     * 批量删除IP池
     *
     * @param ids 需要删除的IP池主键
     * @return 结果
     */
    @Override
    public int deleteTrainingIpPoolByIds(Long[] ids) {
        return trainingIpPoolMapper.deleteTrainingIpPoolByIds(ids);
    }

    /**
     * 删除IP池信息
     *
     * @param id IP池主键
     * @return 结果
     */
    @Override
    public int deleteTrainingIpPoolById(Long id) {
        return trainingIpPoolMapper.deleteTrainingIpPoolById(id);
    }

    @Override
    public int countTrainingIpPool(TrainingIpPool trainingIpPool) {
        return trainingIpPoolMapper.countTrainingIpPool(trainingIpPool);
    }

    /**
     * 批量插入IP池
     *
     * @param trainingIpPoolDto
     * @return
     */
    @Transactional
    @Override
    public int betweenBatchInsert(TrainingIpPoolDto trainingIpPoolDto) {
        //1.执行插入之前需要判断是否还有容器占用着ip
        TrainingIpPool trainingIpPool = new TrainingIpPool();
        trainingIpPool.setStatus(TrainingIpPoolStatus.USED.getValue());
        int count = countTrainingIpPool(trainingIpPool);
        if (count > 0) {
            throw new ServiceException(TrainingIpPoolInfo.PLEASE_RELEASE_ALL_IP.getInfo());
        }

        //2.需要清除所有已存在的ip
        trainingIpPoolMapper.deleteAll();

        //3.添加新的ip
        List<String> ips = getStrings(trainingIpPoolDto);
        return trainingIpPoolMapper.batchInsert(ips);
    }

    private static List<String> getStrings(TrainingIpPoolDto trainingIpPoolDto) {
        List<String> ips = new ArrayList<>();
        int index = trainingIpPoolDto.getStartIp().lastIndexOf('.');
        if (index == -1) {
            throw new ServiceException(TrainingIpPoolInfo.PARAMS_IP_VALID.getInfo());
        }
        int startNum = Integer.parseInt(trainingIpPoolDto.getStartIp().substring(index + 1));
        String prefix = trainingIpPoolDto.getStartIp().substring(0, index + 1);
        for (int i = 0; i < trainingIpPoolDto.getRange(); i++) {
            ips.add(prefix + (startNum + i));
        }
        return ips;
    }
}
